# coding: utf8

def config():
    e = Enforce()
    enforce_buttons = {
        i: SPAN(e.make_enforce_button(i), _id = 'enforce_button_{}'.format(i)) \
        for i in modes.keys()}
    
        
    response.js = make_response_js('button_config')
        
    return TABLE(
        TR(TD('Firewall is '), TD(enforce_buttons['active']), _style = 'background-color: {}'.format(table_colors['row_even'])),
        TR(TD('ICMP enforcement is '), TD(enforce_buttons['icmp']), _style = 'background-color: {}'.format(table_colors['row_even'])),
        TR(TD('TCP enforcement is '), TD(enforce_buttons['tcp']), _style = 'background-color: {}'.format(table_colors['row_even'])),
        TR(TD('UDP enforcement is '), TD(enforce_buttons['udp']), _style = 'background-color: {}'.format(table_colors['row_even'])),
        )
        
def log():
    logs = Logs()
    f = session.log_from = session.log_from or 0
    q = session.log_quantity = session.log_quantity or 10
    
    response.js = make_response_js('button_log')
    return DIV(
        H4('Showing ', SPAN(
                SELECT(5, 10, 20, 50, 100, _style = 'width: auto; font-size: large; color: black', 
                       value = session.log_quantity,
                       _onchange = 'ajax("{}/" + jQuery("#log_quantity select option:selected").text(), [], "log_display");'.format(URL('log','change_quantity'))),
                _id = 'log_quantity'),
            ' records, from ', SPAN(f + 1, _id = 'log_from'),
            ' to ', SPAN(f + q, _id = 'log_to'),
            ', out of ', SPAN(len(logs.logs), _id = 'log_len')),
        A('Refresh', _class = 'button', cid = 'log_display', _href = URL('log', 'refresh')),
        A('Clear', _class = 'button', cid = 'log_display', _href = URL('log', 'clear')),
        A('<<', _class = 'button', cid = 'log_display', _href = URL('log', 'first')),
        A('<', _class = 'button', cid = 'log_display', _href = URL('log', 'prev')),
        A('>', _class = 'button', cid = 'log_display', _href = URL('log', 'next')),
        A('>>', _class = 'button', cid = 'log_display', _href = URL('log', 'last')),
        DIV(logs.get_logs_as_table(f, f + q), _id = 'log_display'),
        )
    
def rules():
    rules = Rules()
    
    response.js = make_response_js('button_rules')
    
    return DIV(H4('Rules:'), DIV(rules.get_as_table(), _id = 'rules_display'))

  
def back():
    response.js = make_response_js('button_back')
    
    return ''

# smooth transitions
def make_response_js(opt):
    return ' '.join(['jQuery("#{}").{}("slow");'.format(i, 'hide' if opt == i else 'show') \
        for i in ('button_back', 'button_config', 'button_log', 'button_rules', 'display')])
